﻿using System;
using System.Linq;
using System.Collections.Generic;
using Fierce.Entity;
using Fierce.Utility.Helper;
using Fierce.Core.Aop;
using Fierce.Core.DbSet;

namespace Fierce.Core.Service
{
    /// <summary>
    /// 基本业务基类
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    /// <typeparam name="R">仓储接口</typeparam>
    public abstract class BaseService<T> : AopTarget where T : EntityBase, new()
    {
        /// <summary>
        /// 当前数据对象
        /// </summary>
        private BaseDbSet<T> dbSet
        {
            get { return db.DbSet<T, BaseDbSet<T>>(); }
        }

        /// <summary>
        /// 保存变更
        /// </summary>
        /// <returns></returns>
        internal bool SaveChanges()
        {
            try
            {
                db.SaveChanges(); return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error(this, ex, ex.Message);
            }
            return false;
        }

        /// <summary>
        /// 保存并回馈
        /// </summary>
        /// <returns></returns>
        internal ServiceResult SaveAndResult(string message = "操作成功", string failure = "操作失败")
        {
            if (SaveChanges()) { return SuccessResult(message); } else { return FailureResult(failure); }

        }

        /// <summary>
        /// 主键查询
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns>实体</returns>
        public virtual T Get(string key)
        {
            return dbSet.Get(key);
        }

        /// <summary>
        /// 全部列表
        /// </summary>
        /// <returns>列表</returns>
        public virtual List<T> GetAll()
        {
            return dbSet.Query().ToList();
        }

        /// <summary>
        /// 保存实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>操作结果</returns>
        public virtual ServiceResult Save(T entity)
        {
            if (string.IsNullOrEmpty(entity.ID))
            {
                return Insert(entity);
            }
            else { return Update(entity); }
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>操作结果</returns>
        public virtual ServiceResult Insert(T entity)
        {
            dbSet.Added(entity); return SaveAndResult("添加成功");
        }

        /// <summary>
        /// 批量添加实体
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>操作结果</returns>
        public virtual ServiceResult Insert(IEnumerable<T> entities)
        {
            dbSet.Added(entities); return SaveAndResult("添加成功");
        }

        /// <summary>
        /// 更改实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>操作结果</returns>
        public virtual ServiceResult Update(T entity)
        {
            dbSet.Modified(entity); return SaveAndResult("修改成功");
        }

        /// <summary>
        /// 批量更改实体
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>操作结果</returns>
        public virtual ServiceResult Update(IEnumerable<T> entities)
        {
            dbSet.Modified(entities); return SaveAndResult("修改成功");
        }

        /// <summary>
        /// 主键删除实体
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns>操作结果</returns>
        public virtual ServiceResult Delete(string key)
        {
            dbSet.Deleted(key); return SaveAndResult("删除成功");
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>操作结果</returns>
        public virtual ServiceResult Delete(T entity)
        {
            dbSet.Deleted(entity); return SaveAndResult("删除成功");
        }

        /// <summary>
        /// 批量删除实体
        /// </summary>
        /// <param name="entities">实体</param>
        /// <returns>操作结果</returns>
        public virtual ServiceResult Delete(IEnumerable<T> entities)
        {
            dbSet.Deleted(entities); return SaveAndResult("删除成功");
        }
    }
}